package page;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import app.WebApp;
import dao.FlightDao;
import entity.Flight;

public class AddFlightPage extends WebPage {

	public AddFlightPage(final Flight flight) {
		super();
		
		final Form<Flight> form = new Form<Flight>("form", new CompoundPropertyModel<Flight>(flight));
		add(form);
		form.add(new TextField<String>("route"));
		form.add(new TextField<String>("time"));
		form.add(new TextField<String>("plane"));
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				final FlightDao fligthDao = new FlightDao(WebApp.DATA_XML_FOLDER);
				
				if (flight.getId() != null) {
					fligthDao.saveNew(flight);
				} else {
					fligthDao.update(flight);
				}
				
				setResponsePage(new AdminFlightListPage());
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new AdminFlightListPage());
			}
		});
	}
}
